import {Component, OnInit} from '@angular/core';
import {LoadingService} from '../../../shared/loading.service';
import {ActivityService} from '../activity.service';
import {AlertService} from '../../../shared/alert.service';
import {ActivatedRoute} from '@angular/router';

@Component({
    selector: 'app-activity-view',
    templateUrl: './activity-view.component.html',
    styleUrls: ['./activity-view.component.scss']
})
export class ActivityViewComponent implements OnInit {

    public dataList: any = [];
    public id: any = '';
    public userData: any = [];

    constructor(public loading: LoadingService,
                public service: ActivityService,
                public alert: AlertService,
                public router: ActivatedRoute) {
    }

    ngOnInit() {
        this.router.params.subscribe(params => {
            this.id = params.id;
        });
        this.getData();
    }

    // 获取列表数据
    getData() {
        this.loading.show();
        const params: any = {
            pk: this.id
        };
        console.log(params);
        this.service.activityDetail(params).subscribe(
            res => {
                this.loading.hide();
                console.log(res, '活动详情数据');
                if (res['code'] === 0) {
                    this.dataList = res['data'];
                    this.userData = res['data']['originator_id'];
                    this.dataList['activity_time'] = this.formatDateTime(this.dataList['activity_time'].substring(0, 10) * 1000);
                    this.dataList['limit_time'] = this.formatDateTime(this.dataList['limit_time'].substring(0, 10) * 1000);
                    this.dataList['add_time'] = this.formatDateTime(this.dataList['add_time'].substring(0, 10) * 1000);
                    if (this.dataList['activity_img'] || this.dataList['activity_img'] !== null) {
                        this.dataList['activity_img'] = this.dataList['activity_img'].substring(1, this.dataList['activity_img'].length - 1).split(', ');
                        for (let k = 0; k < this.dataList['activity_img'].length; k++) {
                            this.dataList['activity_img'][k] = IMG_URL + this.dataList['activity_img'][k].substring(1, this.dataList['activity_img'][k].length - 1);
                        }
                    } else {
                        this.dataList['activity_img'] = '';
                    }
                } else {
                    this.alert.show(res['msg'])
                }
            },
            error => {
                this.loading.hide();
                this.alert.show('网络错误');
            }
        );
    }

    formatDateTime(inputTime) {
        const time = new Date(parseInt(inputTime));
        const y = time.getFullYear();
        const m = time.getMonth() + 1;
        const min = m < 10 ? ('0' + m) : m;
        const d = time.getDate();
        const day = d < 10 ? ('0' + d) : d;
        const h = time.getHours();
        const hours = h < 10 ? ('0' + h) : h;
        const minute = time.getMinutes();
        const second = time.getSeconds();
        const minutes = minute < 10 ? ('0' + minute) : minute;
        const seconds = second < 10 ? ('0' + second) : second;
        // return y + '.' + min + '.' + day;
        return y + '-' + min + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
    };

    back() {
        window.history.go(-1);
    }

}
